package main

import (
	"log"
	"net"
	"patient-srv/basic/config"
	"patient-srv/basic/inits"
	proto "patient-srv/basic/proto"
	"patient-srv/handler/model"
	"patient-srv/handler/service"

	"google.golang.org/grpc"
)

func main() {
	// 初始化配置
	inits.InitConfig()

	// 初始化MySQL
	inits.InitMySQL()

	// 初始化Redis
	inits.InitRedis()

	// 自动迁移表结构
	config.GlobalDB.AutoMigrate(
		&model.User{},
		&model.Patient{},
		&model.Address{},
		&model.Order{},
		&model.Appointment{},
		&model.Consultation{},
		&model.PrescriptionRequest{},
		&model.PrescriptionMedicine{},
		&model.Hospital{},
		&model.Doctor{},
	)

	// 创建 gRPC 服务
	server := grpc.NewServer()

	// 注册服务
	proto.RegisterPatientServiceServer(server, &service.PatientServiceServer{})

	// 监听端口
	addr := "127.0.0.1:50054"
	listener, err := net.Listen("tcp", addr)
	if err != nil {
		log.Fatalf("failed to listen: %v", err)
	}

	log.Printf("Patient Service started on %s", addr)

	// 启动服务
	if err := server.Serve(listener); err != nil {
		log.Fatalf("failed to serve: %v", err)
	}
}
